package com.liu.aliyuncs;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.util.ResourceBundle;

/**
 * @description: TODO
 * @author: zengjiahuan
 * @data: 2019/6/2 20:18
 * @version: V1.0
 **/
public class CommonRpc {
	// 模版CODE
	private static ResourceBundle resb = null;
	private static String domain;
	private static String version;
	// 区域
	private static String regionId;
	// 签名名称;
	private static String signName;
	// 模版CODE
	private static String templateCode;
	// 私钥
	private static String accessKeyId;
	private static String accessSecret;

	static {
		resb = ResourceBundle.getBundle("sms");
		System.out.println("sms");
		domain = resb.getString("ali.domain");
		version = resb.getString("ali.version");
		regionId = resb.getString("ali.RegionId");
		signName = resb.getString("ali.SignName");
		templateCode = resb.getString("ali.TemplateCode");
		accessKeyId = resb.getString("ali.accessKeyId");
		accessSecret = resb.getString("ali.accessSecret");
		System.out.println(signName);
	}

	public static void main(String[] args) {
		String code = "{\"TaskName\":\"" + 0 + "\"," + "\"RecordTime\":\"" + 1 + "\"," + "\"NowValue\":\"" + 2 + "\","
				+ "\"AlarmValue\":\"" + 4 + "\"," + "\"Judgement\":\"" + 5 + "\"," + "\"IP\":\"" + 6 + "\"}";
		sendMessage("liuboTesting", "18692809591");
	}

	public static String sendMessage(String code, String phone) {
		System.out.println("send msg");
		String error = "";
		if (code == null || code.trim().length() == 0 || phone == null || phone.trim().matches("\\d{11}") == false) {
			System.out.println("参数错误：code:" + code + ";phone:" + phone);
			return "参数错误";
		}
		boolean ret = false;
		DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessSecret);
		IAcsClient client = new DefaultAcsClient(profile);
		CommonRequest request = new CommonRequest();
		request.setMethod(MethodType.POST);
		request.setDomain(domain);
		request.setVersion(version);
		request.setAction("SendSms");
		request.putQueryParameter("RegionId", regionId);
		request.putQueryParameter("PhoneNumbers", phone.trim());
		request.putQueryParameter("SignName", signName);
		request.putQueryParameter("TemplateCode", templateCode);
		request.putQueryParameter("TemplateParam", "{\"code\":\"" + code.trim() + "\"}");
		try {
			CommonResponse response = client.getCommonResponse(request);
			System.out.println(response.getData());
			JSONObject jo = JSONObject.parseObject(response.getData());
			if (jo.containsKey("Code") && "OK".equals(jo.getString("Code"))) {
				ret = true;
			} else {
				System.out.println(response.getData());
				if (jo.containsKey("Message")) {
					error = jo.getString("Message");
				}
			}
		} catch (ServerException e) {
			e.printStackTrace();
			System.out.println(e);
			error = "ServerException";
		} catch (ClientException e) {
			e.printStackTrace();
			System.out.println(e);
			System.out.println(e.getMessage());
			error = "ClientException";
		}
		if (ret) {
			return "success";
		} else {
			return error;
		}
	}

}
